﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlutoStudio.EntityFramework
{
    public class DefaultContextInitialization
    {
        public static void Initialization()
        {
            MefContainer.Register<IEntityMapper>(s => s.Inherited());
            MefContainer.Register<ContextBase,DbContext>().ImportProperty(c => c.EntityMappers).SelectConstructor(b=> new ContextBase());
            MefContainer.Builder.ForType<UnitOfWorkContextBase>().ImportProperty(p => p.Context).Export<IUnitOfWork>();
        }
        public static void Initialization(string connectString)
        {
            MefContainer.SetService("nameOrConnectionString", connectString);
            MefContainer.Register<IEntityMapper>(s => s.Inherited());
            MefContainer.Register<ContextBase,DbContext>()
                .ImportProperty(c => c.EntityMappers)
                .SelectConstructor(c => c.First(s => s.GetParameters().Any(p => p.Name == "nameOrConnectionString")), (p, b) =>
                    {
                        b.AsContractName("nameOrConnectionString");
                    });
            MefContainer.Register<UnitOfWorkContextBase,IUnitOfWork>().ImportProperty(p => p.Context);
        }
    }
}
